﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTBLL;
using JWBST.CommonClass;

namespace XWG.JWBST.JWBSTManagementApp.GAXX
{
    public partial class WebUserSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            labMsg.Text = "";
            if (Session["checkGA"] == null)
            {
                Response.Redirect("WebUserSelect.aspx");
            }
            if (txtyanzhengma.Value.FilteHtml().ToString() != Session["checkGA"].ToString())
            {
                labMsg.Text = "请填写正确的验证码";
                return;
            }
            string lsh = this.texName.Value.FilteHtml().Trim();
            string pass = this.texpass.Value.FilteHtml().Trim();
            TbBstCaseApply app = new TbBstCaseApply(); // TbBstCaseApplyBLL.Getapply(lsh, pass);

            if (app != null)
            {
                Log(app.Id, "2");
                if (app.Isaudit == "2")
                {
                    //TbBstCaseTodoBLL.UpdateTodoByapplyid(app.Id);
                    //Response.Redirect("../GAXX/TbBstReturnForUser.aspx?appid=" + app.Id);             
                    TbBstCaseReturn caseReturn = TbBstCaseReturnBLL.GetCaseReturn(app.Id);
                    if (caseReturn != null)
                    {
                        divSearch.Visible = false;
                        if (caseReturn.Casetype.ToString() == "01")
                        {
                            //刑事案件
                            divXS.Visible = true;
                            this.LabFileName.InnerText = caseReturn.FileName.FilteNullReturnEmpty();
                            //this.LabCasetype.InnerText = "刑事案件";
                            //this.LabFileType.InnerText = caseReturn.FileType.FilteNullReturnEmpty();
                            this.LabFileCompany.InnerText = caseReturn.FileCompany.FilteNullReturnEmpty();
                            this.LabFilePolice.InnerText = caseReturn.FilePolice.FilteNullReturnEmpty();
                            this.LabFileDate.InnerText = caseReturn.FileDate.FilteNullReturnEmpty();
                            //this.LabBailName.InnerText = caseReturn.BailName.FilteNullReturnEmpty();
                            this.LabBailDate.InnerText = caseReturn.BailDate.FilteNullReturnEmpty();
                            //this.LabMonitorName.InnerText = caseReturn.MonitorName.FilteNullReturnEmpty();
                            this.LabMonitorDate.InnerText = caseReturn.MonitorDate.FilteNullReturnEmpty();
                            //this.LabDetentionName.InnerText = caseReturn.DetentionName.FilteNullReturnEmpty();
                            this.LabDetentionDate.InnerText = caseReturn.DetentionDate.FilteNullReturnEmpty();
                            this.LabDetentionPlace.InnerText = caseReturn.DetentionPlace.FilteNullReturnEmpty();
                            //this.LabArrestName.InnerText = caseReturn.ArrestName.FilteNullReturnEmpty();
                            this.LabArrestDate.InnerText = caseReturn.ArrestDate.FilteNullReturnEmpty();
                            this.LabArrestPlace.InnerText = caseReturn.ArrestPlace.FilteNullReturnEmpty();
                            //this.LabSueName.InnerText = caseReturn.SueName.FilteNullReturnEmpty();
                            this.LabSueDate.InnerText = caseReturn.SueDate.FilteNullReturnEmpty();
                            this.LabSueProcuratorate.InnerText = caseReturn.SueProcuratorate.FilteNullReturnEmpty();
                        }
                        else
                        {
                            divXZ.Visible = true;
                            //行政案件
                            //this.LabAdAcceptType.InnerText = "行政案件";
                            this.LabAdAcceptName.InnerText = caseReturn.AdAcceptName.FilteNullReturnEmpty();
                            //this.LabAdAcceptType.InnerText = caseReturn.AdAcceptType.FilteNullReturnEmpty();
                            this.LabAdAcceptCompany.InnerText = caseReturn.AdAcceptCompany.FilteNullReturnEmpty();
                            this.LabAdAcceptPolice.InnerText = caseReturn.AdAcceptPolice.FilteNullReturnEmpty();
                            this.LabAdAcceptDate.InnerText = caseReturn.AdAcceptDate.FilteNullReturnEmpty();
                            //this.LabAdPenaltyName.InnerText = caseReturn.AdPenaltyName.FilteNullReturnEmpty();
                            this.LabAdPenaltyResult.InnerText = caseReturn.AdPenaltyResult.FilteNullReturnEmpty();
                        }
                        //更新todo
                        var todo = TbBstCaseTodoBLL.hasApplyid(app.Id);
                        if (todo == null)
                        {
                            todo = new TbBstCaseTodo();
                            todo.Applyid = app.Id;
                            todo.Id = Guid.NewGuid().ToString();
                            todo.Movesign = "0";
                            todo.Status = "0";
                            todo.Lastsearchdate = DateTime.Now;
                            TbBstCaseTodoBLL.Insert(todo, "todoinsert", false);
                        }
                        else
                        {
                            todo.Movesign = "0";
                            todo.Status = "0";
                            todo.Lastsearchdate = DateTime.Now;
                            TbBstCaseTodoBLL.UpdateTodoByapplyid(app.Id);
                        }
                    }
                    else
                    {
                        Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"无法查询到反馈信息\")</script>");
                    }


                }
                else if (app.Isaudit == "1")
                {
                    Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"您注册的身份信息与《受案登记表》中的信息不符，审核未通过\")</script>");
                }
                else
                {
                    Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"该申请信息正在等候审核，请稍后查询\")</script>");
                }
            }
            else
            {
                Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"查询失败：流水号或密码错误或不存在\")</script>");
            }
        }

        public void Log(string appid, string opType)
        {
            TbBstCaseLog log = new TbBstCaseLog();
            log.Id = Guid.NewGuid().ToString();
            log.OpDate = DateTime.Now.ToString();
            log.OpType = opType;
            log.Ipaddress = GetWebClientIp();
            log.Applyid = appid;
            TbBstCaseLogBLL.Insert(log, "", false);
        }

        #region 获取web客户端ip
        /// <summary>
        /// 获取web客户端ip
        /// </summary>
        /// <returns></returns>
        public static string GetWebClientIp()
        {

            string userIP = "未获取用户IP";

            try
            {
                if (System.Web.HttpContext.Current == null
            || System.Web.HttpContext.Current.Request == null
            || System.Web.HttpContext.Current.Request.ServerVariables == null)
                    return "";

                string CustomerIP = "";

                //CDN加速后取到的IP simone 090805
                CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                if (!string.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }

                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


                if (!String.IsNullOrEmpty(CustomerIP))
                    return CustomerIP;

                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    if (CustomerIP == null)
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                else
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                }

                if (string.Compare(CustomerIP, "unknown", true) == 0)
                    return System.Web.HttpContext.Current.Request.UserHostAddress;
                return CustomerIP;
            }
            catch { }

            return userIP;

        }
        #endregion
    }
}